home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Shark Attack / Sources & Headers / Stats.c < prev    next >
Encoding:
Text File  |  1998-04-22  |  6.9 KB  |  240 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Stats.c    - routines for displaying the stats portion of the screen
  3. //
  4. // Created 9/20/97
  5. ///--------------------------------------------------------------------------------------
  6.  
  7. #include <SWIncludes.h>
  8. #include <SWGameUtils.h>
  9. #include <SWApplication.h>
  10.  
  11. #include "Application.h"
  12. #include "Stats.h"
  13. #include "Shark Attack.h"
  14. #include "GlobalVariables.h"
  15.  
  16.  
  17. StatsRec    gLivesRec, gLevelRec, gScoreRec;
  18.  
  19.  
  20. ///--------------------------------------------------------------------------------------
  21. //  InitStats - create offscreen and window frames for stats area. This function is
  22. //    called only once when the program first start up.
  23. ///--------------------------------------------------------------------------------------
  24.  
  25. void    InitStats( void )
  26. {
  27.     PixPatHandle    statsPixPatH;
  28.     Rect            statsRect;
  29.     OSErr            err;
  30.     
  31.     statsRect = gSpriteWorldP->windRect;
  32.     statsRect.bottom = statsRect.top + kStatsHeight;
  33.     
  34.     SetPort(gWindowP);
  35.     err = SWCreateWindowFrame(&gStatsWindowFrameP, &statsRect, 0);
  36.     FatalError(err);
  37.     
  38.     OffsetRect(&statsRect, -statsRect.left, -statsRect.top);
  39.     err = SWCreateFrame(gSpriteWorldP->mainSWGDH, &gStatsBackFrameP, &statsRect, 
  40.             gSpriteWorldP->pixelDepth);
  41.     FatalError(err);
  42.     
  43.     SWLockWindowFrame(gStatsWindowFrameP);
  44.     SWLockFrame(gStatsBackFrameP);
  45.     
  46.     statsPixPatH = GetPixPat(131);
  47.     if (statsPixPatH == NULL)
  48.         CantFindResource();
  49.     
  50.         // Fill stats area with grey
  51.     statsRect = gStatsBackFrameP->frameRect;
  52.     SetGWorld(gStatsBackFrameP->framePort, nil);
  53.     FillCRect(&statsRect, statsPixPatH);
  54.     DisposePixPat(statsPixPatH);
  55.     
  56.         // Draw a black & white frame around the stats area
  57.     ForeColor(whiteColor);
  58.     FrameRect(&statsRect);
  59.     
  60.     InsetRect(&statsRect, 1, 1);
  61.     ForeColor(blackColor);
  62.     FrameRect(&statsRect);
  63.     
  64.         // Set certain fields of each StatsRec
  65.     gLivesRec.theFrameP = gStatsWindowFrameP;
  66.     gLivesRec.numDigits = 1;
  67.     gLivesRec.justification = kRightJustify;
  68.     
  69.     gLevelRec.theFrameP = gStatsWindowFrameP;
  70.     gLevelRec.numDigits = 2;
  71.     gLevelRec.justification = kRightJustify;
  72.  
  73.     gScoreRec.theFrameP = gStatsWindowFrameP;
  74.     gScoreRec.numDigits = 5;
  75.     gScoreRec.justification = kLeftJustify;
  76.     
  77.         // Load the picture for each area (lives, level, and score)
  78.     DrawPictInStatsArea(kLivesPictResID);
  79.     DrawPictInStatsArea(kLevelPictResID);
  80.     DrawPictInStatsArea(kScorePictResID);
  81.     
  82.         // Load the picture containing our digits into a Frame
  83.     err = SWCreateFrameFromPictResource(gSpriteWorldP, &gStatsNumberFrameP, 
  84.             kNumbersPictResID, 0, kNoMask);
  85.     FatalError(err);
  86.     
  87.     SWLockFrame(gStatsNumberFrameP);
  88. }
  89.  
  90.  
  91. ///--------------------------------------------------------------------------------------
  92. //  DrawPictInStatsArea - used by InitStats. Assumes correct port is already set.
  93. ///--------------------------------------------------------------------------------------
  94.  
  95. void    DrawPictInStatsArea( short pictID )
  96. {
  97.     PicHandle     myPictH;
  98.     Rect        pictRect;
  99.  
  100.     myPictH = GetPicture(pictID);
  101.     if (myPictH != NULL)
  102.     {
  103.         pictRect = (**myPictH).picFrame;
  104.         CenterRect(&pictRect, &gStatsBackFrameP->frameRect);
  105.         
  106.             // Determine where to draw pict, and save position for drawing numbers later
  107.         if (pictID == kLivesPictResID)
  108.         {
  109.             OffsetRect(&pictRect, -pictRect.left + 40, 0);
  110.             gLivesRec.horizLoc = pictRect.left + kLivesNumOffset;
  111.             gLivesRec.vertLoc = pictRect.top + kStatsVertOffset;
  112.         }
  113.         else if (pictID == kLevelPictResID)
  114.         {
  115.             OffsetRect(&pictRect, -20, 0);
  116.             gLevelRec.horizLoc = pictRect.left + kLevelNumOffset;
  117.             gLevelRec.vertLoc = pictRect.top + kStatsVertOffset;
  118.         }
  119.         else    // kScorePictResID
  120.         {
  121.             OffsetRect(&pictRect, gStatsBackFrameP->frameRect.right - 
  122.                     pictRect.right - 40, 0);
  123.             gScoreRec.horizLoc = pictRect.left + kScoreNumOffset;
  124.             gScoreRec.vertLoc = pictRect.top + kStatsVertOffset;
  125.         }
  126.         
  127.         DrawPicture(myPictH, &pictRect);
  128.         ReleaseResource((Handle)myPictH);
  129.     }
  130.     else
  131.         CantFindResource();
  132. }
  133.  
  134.  
  135. ///--------------------------------------------------------------------------------------
  136. //  UpdateStatsArea - copies the contents of the gStatsBackFrameP to the gStatsWindowFrameP
  137. ///--------------------------------------------------------------------------------------
  138.  
  139. void    UpdateStatsArea( void )
  140. {
  141.     SetGWorld(gStatsWindowFrameP->framePort, nil);
  142.         
  143.     (*gSpriteWorldP->screenDrawProc)(gStatsBackFrameP, gStatsWindowFrameP,
  144.             &gStatsBackFrameP->frameRect, &gStatsWindowFrameP->frameRect);
  145.     
  146.     ResetStats();    // Force the numbers to redraw, by making them think they've changed
  147.     UpdateStatsNumbers();    // Then draw them
  148. }
  149.  
  150.  
  151. ///--------------------------------------------------------------------------------------
  152. //  UpdateStatsNumbers - redraws the numbers for each stats area if they've changed
  153. ///--------------------------------------------------------------------------------------
  154.  
  155. void    UpdateStatsNumbers( void )
  156. {
  157.         // Stuff lives, level, and score data into each StatsRec
  158.     gLivesRec.theNum = gNumLivesLeft;
  159.     gLevelRec.theNum = gCurrentLevel;
  160.     gScoreRec.theNum = gScore;
  161.     
  162.     UpdateStatsRec(&gLivesRec);
  163.     UpdateStatsRec(&gLevelRec);
  164.     UpdateStatsRec(&gScoreRec);
  165. }
  166.  
  167.  
  168. ///--------------------------------------------------------------------------------------
  169. //  ResetStats - called before a new game to make sure the new stats get drawn
  170. ///--------------------------------------------------------------------------------------
  171.  
  172. void    ResetStats( void )
  173. {
  174.     gLivesRec.oldNum = -1;
  175.     gLevelRec.oldNum = -1;
  176.     gScoreRec.oldNum = -1;
  177. }
  178.  
  179.  
  180. ///--------------------------------------------------------------------------------------
  181. //  UpdateStatsRec - redraws the number for a particular statsRec if it has changed
  182. ///--------------------------------------------------------------------------------------
  183.  
  184. void    UpdateStatsRec( StatsRecPtr statsRecP )
  185. {
  186.     short    theRow, theCol, theNum, theDigit;
  187.     short    length = 0;
  188.     Rect    srcRect, dstRect;
  189.     
  190.         // Only draw the number if it has changed since last time
  191.     if (statsRecP->oldNum != statsRecP->theNum)
  192.     {
  193.         statsRecP->oldNum = statsRecP->theNum;
  194.         
  195.         theRow = statsRecP->theFrameP->frameRect.top + statsRecP->vertLoc;
  196.         theCol = statsRecP->theFrameP->frameRect.left + statsRecP->horizLoc;
  197.         theNum = statsRecP->theNum;
  198.         if (theNum < 0)
  199.             theNum = 0;
  200.         
  201.         do    // calculate length of the number
  202.         {
  203.             length++;
  204.             theNum /= 10;
  205.         } while (theNum > 0);
  206.         
  207.         if (statsRecP->justification == kLeftJustify)
  208.             theCol += (length-1) * kNumberWidth;
  209.         else
  210.             theCol += (statsRecP->numDigits-1) * kNumberWidth;
  211.         
  212.         theNum = statsRecP->theNum;
  213.         if (theNum < 0)
  214.             theNum = 0;
  215.         
  216.         do
  217.         {
  218.             theDigit = theNum%10;
  219.             
  220.             srcRect.top = 0;
  221.             srcRect.bottom = kNumberHeight;
  222.             srcRect.left = theDigit * kNumberWidth;
  223.             srcRect.right = srcRect.left + kNumberWidth;
  224.             
  225.             dstRect.left = theCol;
  226.             dstRect.top = theRow;
  227.             dstRect.right = theCol + kNumberWidth;
  228.             dstRect.bottom = theRow + kNumberHeight;
  229.             
  230.                 // Copy the digit using the SpriteWorld's current screenDrawProc
  231.             (*gSpriteWorldP->screenDrawProc)(gStatsNumberFrameP, gStatsWindowFrameP,
  232.                     &srcRect, &dstRect);
  233.             
  234.             theCol -= kNumberWidth;
  235.             theNum /= 10;
  236.         } while (theNum > 0);
  237.     }
  238. }
  239.  
  240.